11 $wgMagicFound = false;
13 /** Actual keyword to be used is set in Language.php */
20 'MAG_CURRENTMONTHNAME',
21 'MAG_CURRENTMONTHNAMEGEN',
22 'MAG_CURRENTMONTHABBREV',
27 'MAG_NUMBEROFARTICLES',
54 'MAG_NOCONTENTCONVERT',
61 'MAG_IMG_MANUALTHUMB',
73 for ( $i = 0; $i < count( $magicWords ); ++
$i )
74 define( $magicWords[$i], $i );
76 $wgVariableIDs = array(
79 MAG_CURRENTMONTHNAMEGEN
,
80 MAG_CURRENTMONTHABBREV
,
103 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
105 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
107 * Possible future improvements:
108 * * Simultaneous searching for a number of magic words
109 * * $wgMagicWords in shared memory
111 * Please avoid reading the data out of one of these objects and then writing
112 * special case code. If possible, add another match()-like function here.
120 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
121 var $mRegexStart, $mBaseRegex, $mVariableRegex;
125 function MagicWord($id = 0, $syn = '', $cs = false) {
127 $this->mSynonyms
= (array)$syn;
128 $this->mCaseSensitive
= $cs;
130 $this->mRegexStart
= '';
131 $this->mVariableRegex
= '';
132 $this->mVariableStartToEndRegex
= '';
133 $this->mModified
= false;
137 * Factory: creates an object representing an ID
140 function &get( $id ) {
141 global $wgMagicWords;
143 if ( !is_array( $wgMagicWords ) ) {
144 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
146 if (!array_key_exists( $id, $wgMagicWords ) ) {
147 $mw = new MagicWord();
149 $wgMagicWords[$id] = $mw;
151 return $wgMagicWords[$id];
154 # Initialises this object with an ID
155 function load( $id ) {
158 $wgContLang->getMagic( $this );
162 * Preliminary initialisation
165 function initRegex() {
166 #$variableClass = Title::legalChars();
167 # This was used for matching "$1" variables, but different uses of the feature will have
168 # different restrictions, which should be checked *after* the MagicWord has been matched,
170 $escSyn = array_map( 'preg_quote', $this->mSynonyms
);
171 $this->mBaseRegex
= implode( '|', $escSyn );
172 $case = $this->mCaseSensitive ?
'' : 'i';
173 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
174 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
175 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
176 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
177 "/^(?:{$this->mBaseRegex})$/{$case}" );
181 * Gets a regex representing matching the word
183 function getRegex() {
184 if ($this->mRegex
== '' ) {
187 return $this->mRegex
;
191 * Gets a regex matching the word, if it is at the string start
193 function getRegexStart() {
194 if ($this->mRegex
== '' ) {
197 return $this->mRegexStart
;
201 * regex without the slashes and what not
203 function getBaseRegex() {
204 if ($this->mRegex
== '') {
207 return $this->mBaseRegex
;
211 * Returns true if the text contains the word
214 function match( $text ) {
215 return preg_match( $this->getRegex(), $text );
219 * Returns true if the text starts with the word
222 function matchStart( $text ) {
223 return preg_match( $this->getRegexStart(), $text );
227 * Returns NULL if there's no match, the value of $1 otherwise
228 * The return code is the matched string, if there's no variable
229 * part in the regex and the matched variable part ($1) if there
232 function matchVariableStartToEnd( $text ) {
233 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
234 if ( $matchcount == 0 ) {
236 } elseif ( count($matches) == 1 ) {
239 # multiple matched parts (variable match); some will be empty because of synonyms
240 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
241 $matches = array_values(array_filter($matches));
248 * Returns true if the text matches the word, and alters the
249 * input string, removing all instances of the word
251 function matchAndRemove( &$text ) {
252 global $wgMagicFound;
253 $wgMagicFound = false;
254 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
255 return $wgMagicFound;
258 function matchStartAndRemove( &$text ) {
259 global $wgMagicFound;
260 $wgMagicFound = false;
261 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
262 return $wgMagicFound;
267 * Replaces the word with something else
269 function replace( $replacement, $subject ) {
270 $res = preg_replace( $this->getRegex(), $replacement, $subject );
271 $this->mModified
= !($res === $subject);
276 * Variable handling: {{SUBST:xxx}} style words
277 * Calls back a function to determine what to replace xxx with
278 * Input word must contain $1
280 function substituteCallback( $text, $callback ) {
281 $regex = $this->getVariableRegex();
282 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
283 $this->mModified
= !($res === $text);
288 * Matches the word, where $1 is a wildcard
290 function getVariableRegex() {
291 if ( $this->mVariableRegex
== '' ) {
294 return $this->mVariableRegex
;
298 * Matches the entire string, where $1 is a wildcard
300 function getVariableStartToEndRegex() {
301 if ( $this->mVariableStartToEndRegex
== '' ) {
304 return $this->mVariableStartToEndRegex
;
308 * Accesses the synonym list directly
310 function getSynonym( $i ) {
311 return $this->mSynonyms
[$i];
315 * Returns true if the last call to replace() or substituteCallback()
316 * returned a modified text, otherwise false.
318 function getWasModified(){
319 return $this->mModified
;
323 * $magicarr is an associative array of (magic word ID => replacement)
324 * This method uses the php feature to do several replacements at the same time,
325 * thereby gaining some efficiency. The result is placed in the out variable
326 * $result. The return value is true if something was replaced.
329 function replaceMultiple( $magicarr, $subject, &$result ){
332 foreach( $magicarr as $id => $replacement ){
333 $mw = MagicWord
::get( $id );
334 $search[] = $mw->getRegex();
335 $replace[] = $replacement;
338 $result = preg_replace( $search, $replace, $subject );
339 return !($result === $subject);
343 * Adds all the synonyms of this MagicWord to an array, to allow quick
344 * lookup in a list of magic words
346 function addToArray( &$array, $value ) {
347 foreach ( $this->mSynonyms
as $syn ) {
348 $array[$syn] = $value;
354 * Used in matchAndRemove()
357 function pregRemoveAndRecord( $match ) {
358 global $wgMagicFound;
359 $wgMagicFound = true;